//
//  ZPFUserManager.m
//  ZPFHimalayan
//
//  Created by zhou on 2018/11/23.
//  Copyright © 2018 MissZhou. All rights reserved.
//

#import "ZPFUserManager.h"

@implementation ZPFUserManager

SINGLETON_FOR_CLASS(ZPFUserManager)

- (instancetype)init
{
    self = [super init];
    if (self) {
        //注册被踢下线通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didKichedOnLineAction)
                                                     name:KNotificationKickedOnLine
                                                   object:nil];
    }
    return self;
}

#pragma mark - 第三方登录
- (void)login:(UserLoginType)loginType completion:(loginBlock)completion{
    [self login:loginType params:nil completion:completion];
}

#pragma mark - 参数登录
- (void)login:(UserLoginType)loginType params:(NSDictionary *)params completion:(loginBlock)completion{
    switch (loginType) {
        case UserLoginTypeIsUnKnow:
        {
            //未知登录，不做处理
        }
            break;
        case UserLoginTypeIsWeChat:
        {
            //微信登录
            [self loginToService:params completion:completion];
        }
            break;
        case UserLoginTypeIsQQ:
        {
            //QQ登录
        }
            break;
        case UserLoginTypeIsAccount:
        {
            //帐号登录
            [self loginToService:params completion:completion];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 登录到服务器
- (void)loginToService:(NSDictionary *)params completion:(loginBlock)completion{
    NSLog(@"网络请求");
}

#pragma mark - 自动登录到服务器
- (void)autoLoginToService:(loginBlock)completion{
    NSLog(@"自动登录到服务器");
}

#pragma mark - 登录成功处理
- (void)loginSuccess:(id)responseObject completion:(loginBlock)completion{
    if (ValidDict(responseObject)) {
        if (ValidDict(responseObject[@"data"])) {
            NSDictionary *data = responseObject[@"data"];
            self.currentUserInfo = [ZPFUserInfo modelWithJSON:data];
            //保存当前用户数据
            [self saveCurrentUserInfo];
            self.isLogin = YES;
            if (completion) {
                completion(YES,nil);
            }
            kPostNotification(KNotificationLoginStateChange, @YES);
        }else{
            if (completion) {
                completion(NO,@"登录返回数据异常");
            }
        }
    }else{
        if (completion) {
            completion(NO,@"登录返回数据异常");
        }
        kPostNotification(KNotificationLoginStateChange, @NO);
    }
}

#pragma mark - 储存当前用户数据
- (void)saveCurrentUserInfo{
    if (self.currentUserInfo) {
        YYCache *cache = [[YYCache alloc] initWithName:KUserCacheName];
        NSDictionary *dic = [self.currentUserInfo modelToJSONObject];
        [cache setObject:dic forKey:KUserCacheModel];
    }
}

#pragma mark - 加载缓存的用户信息
- (BOOL)loadCacheUserInfo{
    YYCache *cache = [[YYCache alloc] initWithName:KUserCacheName];
    NSDictionary *userDic = (NSDictionary *)[cache objectForKey:KUserCacheModel];
    if (userDic) {
        self.currentUserInfo = [ZPFUserInfo modelWithJSON:userDic];
        return YES;
    }
    return NO;
}

#pragma mark - 退出登录
- (void)logout:(loginBlock)completion{
    //推送数量置为0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //注销通知
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    self.isLogin = NO;
    self.currentUserInfo = nil;
    
    YYCache *cache = [[YYCache alloc] initWithName:KUserCacheName];
    [cache removeAllObjectsWithBlock:^{
        if (completion) {
            completion(YES,nil);
        }
    }];
    kPostNotification(KNotificationLoginStateChange, @NO);
}

#pragma mark - 被踢下线
- (void)didKichedOnLineAction{
    [self logout:nil];
}

@end
